package com.wobito.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wobito.common.utils.DateUtils;
import com.wobito.common.utils.StringUtils;
import com.wobito.mapper.AreaMapper;
import com.wobito.pojo.bean.Area;
import com.wobito.pojo.dto.area.AreaBriefDTO;
import com.wobito.pojo.dto.area.AreaDTO;
import com.wobito.pojo.enums.IsDeletedEnum;
import com.wobito.pojo.vo.area.AddAreaVO;
import com.wobito.pojo.vo.area.AreaVO;
import com.wobito.pojo.vo.area.UpdateAreaVO;
import com.wobito.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 区域表 服务实现类
 * </p>
 *
 * @author lambyang
 * @since 2024-08-26
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List<AreaDTO> selectAreaList(AreaVO areaVO) {
        List<AreaDTO> areaDTOS = new ArrayList<>();
        List<Area> areas = areaMapper.selectList(new LambdaQueryWrapper<Area>()
                .or(StringUtils.isNotEmpty(areaVO.getQueryName()), w -> {
                    w.like(Area::getName, areaVO.getQueryName());
                    w.or().like(Area::getAddress, areaVO.getQueryName());
                })
                .eq(areaVO.getIsDeleted() != null, Area::getIsDeleted, areaVO.getIsDeleted()));
        if (CollectionUtil.isNotEmpty(areas)) {
            areaDTOS = areas.stream().map(a -> {
                AreaDTO areaDTO = BeanUtil.copyProperties(a, AreaDTO.class);
                return areaDTO;
            }).collect(Collectors.toList());
        }
        return areaDTOS;
    }

    @Override
    public List<AreaBriefDTO> selectBrief() {
        List<AreaBriefDTO> areaBriefDTOS=new ArrayList<>();
        List<Area> areas=areaMapper.selectList(new LambdaQueryWrapper<Area>().eq(Area::getIsDeleted, IsDeletedEnum.NORMAL.getCode()));
        if (CollectionUtil.isNotEmpty(areas)){
            areaBriefDTOS=areas.stream().map(a->{
                AreaBriefDTO areaBriefDTO=BeanUtil.copyProperties(a,AreaBriefDTO.class);
                return areaBriefDTO;
            }).collect(Collectors.toList());
        }
        return areaBriefDTOS;
    }

    @Override
    public int insertArea(AddAreaVO areaVO) {
        Area area=BeanUtil.copyProperties(areaVO,Area.class);
        return areaMapper.insert(area);
    }

    @Override
    public int updateArea(UpdateAreaVO updateAreaVO) {
        Area area=BeanUtil.copyProperties(updateAreaVO,Area.class);
        area.setUpdateTime(DateUtils.getNowDate());
        return areaMapper.updateById(area);
    }
}
